package com.car.utils;

import com.car.doamin.Car;
import com.car.doamin.Employee;
import com.car.doamin.PurchaseRecord;
import com.car.doamin.User;
import com.car.service.EmployeeService;
import com.car.service.PurchaseRecordService;
import com.car.service.StoreService;
import com.car.service.UserService;

import java.util.List;
import java.util.Scanner;

public class MenuUtil {
    private static final UserService userService = new UserService();
    private static final StoreService storeService = new StoreService();
    private static final PurchaseRecordService purchaseRecordService = new PurchaseRecordService();
    private static final EmployeeService employeeService = new EmployeeService();
    public static void mainMenu() throws Exception {

        Scanner scanner = new Scanner(System.in);
        int n = 0;
        while (true) {
            System.out.println("=============汽车系统=============");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.员工登录");
            System.out.println("0.退出");
            System.out.println("=================================");
            n = scanner.nextInt();
            if (n == 1) {
                //用户登录
                User user = userService.login(scanner);
                if (user != null) {
                    System.out.println("登录成功。");
                    // 登录成功后操作
                    userMenu(user, scanner);
                } else {
                    System.out.println("登录失败");
                }
            } else if (n == 2) {
                // 用户注册
                Boolean isSuccess = userService.register(scanner);
                if (isSuccess) {
                    System.out.println("注册成功，请登录。");
                } else {
                    System.out.println("注册失败");
                }
            } else if (n == 3) {
                // todo 员工登录
                Employee employee = employeeService.login(scanner);
                if (employee != null){
                    System.out.printf("登录成功");
                    employeeMenu(employee,scanner);
                }else {
                    System.out.printf("登录失败");
                }
            } else if (n == 0) {
                break;
            } else {
                System.out.println("输入错误，请重新输入：");
            }
        }
    }

    private static void employeeMenu(Employee employee, Scanner scanner) throws Exception {
        int n;
        while (true) {
            System.out.println("=============员工菜单=============");
            System.out.println("1.查看商店汽车");
            System.out.println("2.上架汽车");
            System.out.println("3.查看购买记录");
            System.out.println("0.退出");
            System.out.println("=================================");
            System.out.println("请输入指令：");
            n = scanner.nextInt();
            if (n == 0) {
                break;
            } else if (n == 1) {
                //查看商店汽车
                printCars(storeService.display());
            }else if (n == 2) {
                //上架汽车
                employeeService.addCar(employee,scanner);
            }else if (n == 3) {
                //查看购买记录
                List<PurchaseRecord> list = purchaseRecordService.findAll();
                for (PurchaseRecord pr : list){
                    System.out.println(pr);
                }
            }else {
                System.out.printf("输入错误");
            }
        }
    }
    /**
     * 用户菜单
     *
     * @param user 登录用户
     */
    private static void userMenu(User user, Scanner scanner) throws Exception {
        int n;
        while (true) {
            System.out.println("=============用户菜单=============");
            System.out.println("1.查看商店汽车");
            System.out.println("2.购买汽车");
            System.out.println("3.查看购买记录");
            System.out.println("4.个人汽车");
            System.out.println("5.修改用户名");
            System.out.println("6.修改密码");
            System.out.println("0.退出");
            System.out.println("=================================");
            System.out.println("请输入指令：");
            n = scanner.nextInt();
            if (n == 0) {
                break;
            } else if (n == 1) {
                //查看商店汽车
                printCars(storeService.display());
            } else if (n == 2) {
                userService.buyCar(user,scanner);
            } else if (n == 3) {
                //查看购买记录
               List<PurchaseRecord> purchaseRecords = purchaseRecordService.findByUserId(user.getId());
               System.out.println(purchaseRecords);
            } else if (n == 4) {
                //个人汽车
               List<Car> carList =  userService.ownCars(user.getId());
               printCars(carList);
            } else if (n == 5) {
                //修改用户名
                userService.updateUserName(user,scanner);
            } else if (n == 6) {
                //修改密码
                userService.updataPassword(user,scanner);
            } else {
                System.out.println("输入错误，请重新输入！");
            }
        }
    }
    private static void printCars(List<Car> list){
        System.out.println("id\tname\tbrand\tprice\ttype\tcolor\tcarNum\tstatus\temployeeId");
        for (Car car:list){
            System.out.println(car.getId()+"\t"+car.getName()+"\t"+car.getBrand()+"\t"+car.getType()
                    +"\t"+car.getColor()+"\t"+car.getCarNum()+"\t"+car.getStatus()+"\t"+car.getEmployeeId());
        }
    }
}
